import "https://cdn.bootcdn.net/ajax/libs/jquery/1.12.4/jquery.min.js";

function login() {
    let username = $('.username');
    let password = $('.password');
    let checkp = $('.checkp');
    let btn = $('.btn');//按钮
    let userflag = true;//标记

    //与数据库连接确认是否有该用户名
    username.on('blur', function() {
        $.ajax({
            type: 'post',
            url: 'http://10.31.155.17/wanmeishijie/src/php/login.php',
            data: {
                user: $(this).val()
            }
        }).done(function(data) {
            if (data === 'false') {
                checkp.eq(0).css('color', 'red').html('用户名不存在');
                userflag = false;
            } else {
                checkp.eq(0).html('');
                userflag = true;
            }
        });
    });


    btn.on('click', function() {
        if (userflag) {
            $.ajax({
                type: 'post',
                url: 'http://10.31.155.17/wanmeishijie/src/php/login.php',
                data: {
                    user: username.val(), //用户名
                    pass: password.val(), //密码
                    // yzm: captchaimg.val() //验证码
                }
            }).done(function(data) {
                if (data === 'success') {
                    // checkp.eq(0).css('color', 'red').html('用户名不存在');
                    // userflag = false;
                    location.href = 'index.html';
                    localStorage.setItem('username',username.val())
                } else {
                    alert('登录错误')
                    userflag = false;
                }
            });
        }
    });

}

export {
    login
}